﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Windows.Forms;

namespace TCSF_Licence
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            AES.Key = "itistcsfpassword";
        }

        private void btnGen_Click(object sender, EventArgs e)
        {
            Licence licence = new Licence();
            Boolean bindCpu = this.cbCpu.Checked;
            licence.BindCpu = bindCpu;
            Boolean bindDisk = this.cbDisk.Checked;
            licence.BindDisk = bindDisk;
            Boolean bindMac = this.cbMac.Checked;
            licence.BindMac = bindMac;
            Boolean showTrial = this.cbTrial.Checked;
            string machineCode = this.tbMachineCode.Text;
            DateTime expiredTime = this.dtExpiredTime.Value;
            int days = (int)this.numDays.Value;
            if (bindCpu || bindDisk || bindMac)
            {
                if (machineCode.Length == 0/* && (machineCode.Length % 2) == 0*/)
                {
                    MessageBox.Show("请输入服务器机器码！");
                    return;
                }
                //  把机器码转换为字节数组
                //  字节数组进行AES解密得到json
                //  把json反序列化为对象
                string sMachineCode = AES.AesDecrypt(machineCode);
                MachineInfo tMachineInfo = JsonSerialization.JsonDeserializationFun<MachineInfo>(sMachineCode);
                licence.CpuSn = tMachineInfo.CpuSn;
                licence.DiskSn = tMachineInfo.DiskSn;
                licence.Mac = tMachineInfo.Mac;
            }

            if (this.rbTimePoint.Checked)
            {
                //  检查日期
                licence.Type = "TIME_POINT";
                licence.ExpiredTime = expiredTime;
            }
            else
            {
                if (days <= 0)
                {
                    MessageBox.Show("可用天数输入不正确！");
                    return;
                }
                licence.Type = "TIME_LENGTH";
                licence.EffectiveTimeLength = days;
            }
            licence.ShowTrial = showTrial;

            //  序列化为json
            //  进行aes加密
            //  转换为大写十六进制字符串
            string sJson = JsonSerialization.JsonSerializationFun(licence);
            string sAES = AES.AesEncrypt(sJson);

            string registerCode = sAES;
            this.tbRegisterCode.Text = registerCode;
        }

        private void rbTimePoint_CheckedChanged(object sender, EventArgs e)
        {
            this.lblExpiredTime.Visible = true;
            this.dtExpiredTime.Visible = true;
            this.lblDays.Visible = false;
            this.numDays.Visible = false;
        }

        private void rbTimeLength_CheckedChanged(object sender, EventArgs e)
        {
            this.lblExpiredTime.Visible = false;
            this.dtExpiredTime.Visible = false;
            this.lblDays.Visible = true;
            this.numDays.Visible = true;
        }

        private void cbBind_CheckedChanged(object sender, EventArgs e)
        {
            Boolean bindCpu = this.cbCpu.Checked;
            Boolean bindDisk = this.cbDisk.Checked;
            Boolean bindMac = this.cbMac.Checked;
            if (!bindCpu && !bindDisk && !bindMac)
            {
                this.tbMachineCode.Enabled = false;
            }
            else
            {
                this.tbMachineCode.Enabled = true;
            }
        }
    }
}
